package com.rtp2p.rtcoresdkdemo.base

import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

abstract class BaseActivity: AppCompatActivity() {
    protected abstract fun initWorkspaceAction(savedInstanceState: Bundle?)
    private val view:View?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        /*保存屏幕常亮*/
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        enableEdgeToEdge()
        initWorkspaceAction(savedInstanceState)
    }

    protected fun setOnApplyWindowInsetsListener(view: View) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    protected fun setOnApplyWindowInsetsListener(view: View, height: Int?) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            //v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            v.setPadding(systemBars.left, height ?: systemBars.top, systemBars.right, height ?: systemBars.bottom)
            insets
        }
    }

    // 隐藏状态栏
    protected fun hideStatusBar(view: View) {
        //activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // 全屏
        setOnApplyWindowInsetsListener(view,0)
        // 设置全屏
        window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window?.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
    }

    // 显示状态栏
    protected fun showStatusBar(view: View) {
        //activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // 清除全屏
        setOnApplyWindowInsetsListener(view,null)

        // 取消全屏
        window?.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
        window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }
}